package com.ledim.ledimview;

import android.app.Activity;
import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import com.facebook.drawee.view.SimpleDraweeView;
import com.ledim.activity.PersonalActivity;
import com.ledim.bean.ChatRoomBean;
import com.ledim.bean.LedimUserBean;
import com.ledim.bean.base.BaseResultDataInfo;
import com.ledim.fragment.LedimUserFragment;
import com.ledim.itf.NoDoubleClickListener;
import com.ledim.service.ApiService;
import com.ledim.service.LedimApiServiceFactory;
import com.ledim.service.LedimHttpObserver;
import com.ledim.utils.DensityUtil;
import com.ledim.utils.FrescoUtil;
import com.ledim.utils.LedimFunctionUtils;
import com.ledim.utils.LedimSpUtils;
import com.ledim.utils.LogUtil;
import com.ledim.utils.ToastUtil;
import com.ledim.widget.view.LoadingBar;
import com.letv.android.young.client.R;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by ccx on 16/6/28.
 * 用户图像View
 */
public class LedimUserIconView extends FrameLayout {

    private SimpleDraweeView mSimpleDraweeView;
    private ImageView mTypeImageView;
    private FrameLayout.LayoutParams photolayoutParams;
    private FrameLayout.LayoutParams typeViewLayoutParams;
    private View fl_photo_bg;
    private ViewGroup.LayoutParams photoBglayoutParams;
    private FragmentActivity mAcyivity;
    private String mUid;
    private final int USER_STATE_NORMAL = 1;//普通
    private final int USER_STATE_CHAT = USER_STATE_NORMAL + 1; //聊天
    private final int USER_STATE_FOLLOW = USER_STATE_CHAT + 1;//关注
    private LoadingBar mLoadingBar;
    private int mUserState;
    private ChatRoomBean mRoom;

    public LedimUserIconView(Context context) {
        super(context);
        initView();
    }

    public LedimUserIconView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public LedimUserIconView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView();
    }

    private void initView() {
        View frame = LayoutInflater.from(getContext()).inflate(R.layout.user_icon_layout, null);
        mSimpleDraweeView = (SimpleDraweeView) frame.findViewById(R.id.iv_user);
        mTypeImageView = (ImageView) frame.findViewById(R.id.iv_user_type);
        photolayoutParams = (FrameLayout.LayoutParams) mSimpleDraweeView.getLayoutParams();
        typeViewLayoutParams = (LayoutParams) mTypeImageView.getLayoutParams();
        fl_photo_bg = frame.findViewById(R.id.fl_photo_bg);
        photoBglayoutParams = fl_photo_bg.getLayoutParams();
        addView(frame);
        setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (LedimSpUtils.isLogin()) {
                    if (!TextUtils.isEmpty(mUid) && !mUid.equals(LedimSpUtils.getUid())) {
                        // 不是自己才有跳转或者弹框
                        if (mUserState != 0) {
                            if (mUserState == USER_STATE_NORMAL) {
                                //跳转个人主页
                                PersonalActivity.jumpPersonalHomePage(getContext(), mUid);
                            } else if (mUserState == USER_STATE_CHAT) {
                                {
                                    getOtherUserData(mRoom.roomId, mUid);
                                }
                            } else if (mUserState == USER_STATE_FOLLOW) {
                                //跳转个人主页
                                PersonalActivity.jumpPersonalHomePage(getContext(), mUid);
                            }
                        }
                    }
                } else {
                    LedimFunctionUtils.showLoginDialog((FragmentActivity) getContext());
                }
            }
        });
    }

    /**
     * 粉丝和关注界面用户头像
     *
     * @param uid
     * @param status
     * @param pic
     */
    public void setFollowUserIcon(String uid, String status, String pic) {
        mUserState = USER_STATE_FOLLOW;
        mUid = uid;
        fl_photo_bg.setBackgroundResource(R.drawable.follow_user_bg);
        photolayoutParams.height = DensityUtil.dip2px(getContext(), 39);
        photolayoutParams.width = DensityUtil.dip2px(getContext(), 39);

        typeViewLayoutParams.height = DensityUtil.dip2px(getContext(), 14);
        typeViewLayoutParams.width = DensityUtil.dip2px(getContext(), 14);
        typeViewLayoutParams.leftMargin = DensityUtil.dip2px(getContext(), 25);

        if (!TextUtils.isEmpty(pic)) {
            FrescoUtil.loadUrl(pic, mSimpleDraweeView);
        }
        if (!TextUtils.isEmpty(status)) {
            mTypeImageView.setImageResource(LedimFunctionUtils.getUserTypeIcon(status));
        }
    }

    /**
     * @param uid
     * @param status
     * @param pic
     * @param isMe
     *         设置用户信息，用于放映厅列表页 展示右下角图标
     */
    public void setUserIcon(String uid, String status, String pic, boolean isMe) {
        mUserState = USER_STATE_NORMAL;
        mUid = uid;
        if (!TextUtils.isEmpty(status)) {
            mTypeImageView.setImageResource(LedimFunctionUtils.getUserTypeIcon(status));
        }
        if (isMe) {
            //iv_icon_bg.setBackgroundResource(R.drawable.room_creater_iconbg);
            //ll_photo_bg.setBackgroundResource(R.drawable.room_creater_iconbg);
            fl_photo_bg.setBackgroundResource(R.color.transparent);
            findViewById(R.id.iv_me).setVisibility(VISIBLE);
            photolayoutParams.height = DensityUtil.dip2px(getContext(), 36);
            photolayoutParams.width = DensityUtil.dip2px(getContext(), 36);
            //photoBglayoutParams.height = DensityUtil.dip2px(getContext(), 44);
            //photoBglayoutParams.width = DensityUtil.dip2px(getContext(), 44);
            typeViewLayoutParams.height = DensityUtil.dip2px(getContext(), 14);
            typeViewLayoutParams.width = DensityUtil.dip2px(getContext(), 14);
            typeViewLayoutParams.bottomMargin = DensityUtil.dip2px(getContext(), 4);
            // mTypeImageView.setImageResource(R.drawable.user_type_me);
            typeViewLayoutParams.leftMargin = DensityUtil.dip2px(getContext(), 30);
        } else {
            fl_photo_bg.setBackgroundResource(R.drawable.rect_round_avatar_bg);
            typeViewLayoutParams.height = DensityUtil.dip2px(getContext(), 12);
            typeViewLayoutParams.width = DensityUtil.dip2px(getContext(), 12);
            photolayoutParams.height = DensityUtil.dip2px(getContext(), 30);
            photolayoutParams.width = DensityUtil.dip2px(getContext(), 30);
            //mTypeImageView.setImageResource(R.drawable.user_type_other);
            typeViewLayoutParams.leftMargin = DensityUtil.dip2px(getContext(), 22);
        }
        if (!TextUtils.isEmpty(pic)) {
            FrescoUtil.loadUrl(pic, mSimpleDraweeView);
        }
    }

    /**
     * @param fragmentActivity
     * @param pic
     *         房主才有设置聊天页用户图像，只有聊天室创建者展示右下角图标
     * @param uid
     * @param isWuc
     *         语C聊天室 聊天室头像一样大
     * @param isMe
     *         自己的发言 大头像
     */
    public void setChatIcon(FragmentActivity fragmentActivity, String pic, String uid,
                            boolean isWuc, boolean isMe, ChatRoomBean room) {
        mUserState = USER_STATE_CHAT;
        mAcyivity = fragmentActivity;
        mRoom = room;
        mUid = uid;

        if (room.user.userId.equals(uid) || isWuc || isMe) {
            if (room.user.userId.equals(uid)) {
                //房主逻辑
                fl_photo_bg.setBackgroundResource(R.drawable.room_creater_iconbg);
            } else {
                if (isWuc || isMe) {
                    //非房主 的语C用户 或者是自己不要圆圈
                    fl_photo_bg.setBackgroundResource(R.color.transparent);
                }
            }

            mTypeImageView.setImageResource(LedimFunctionUtils.getUserTypeIcon(room.user.status));
            //iv_icon_bg.setBackgroundResource(R.drawable.room_creater_iconbg);
            photolayoutParams.height = DensityUtil.dip2px(getContext(), 30);
            photolayoutParams.width = DensityUtil.dip2px(getContext(), 30);
            typeViewLayoutParams.height = DensityUtil.dip2px(getContext(), 12);
            typeViewLayoutParams.width = DensityUtil.dip2px(getContext(), 12);
            // mTypeImageView.setImageResource(R.drawable.user_type_me);
            typeViewLayoutParams.leftMargin = DensityUtil.dip2px(getContext(), 22);
            //mTypeImageView.setImageResource(getUserTypeIcon(status));
        } else {
            fl_photo_bg.setBackgroundResource(R.color.transparent);
            photolayoutParams.height = DensityUtil.dip2px(getContext(), 20);
            photolayoutParams.width = DensityUtil.dip2px(getContext(), 20);
            mTypeImageView.setVisibility(GONE);
        }
        if (!TextUtils.isEmpty(pic)) {
            FrescoUtil.loadUrl(pic, mSimpleDraweeView);
        }

    }

    private void getOtherUserData(String roomId, String uid) {

        if (mLoadingBar == null) {
            mLoadingBar = new LoadingBar((Activity) getContext());
        }
        mLoadingBar.show();
        LedimApiServiceFactory.createService(ApiService.class)
                              .getRoomUserInfo(roomId, uid)
                              .observeOn(AndroidSchedulers.mainThread())
                              .subscribeOn(Schedulers.io())
                              .subscribe(new LedimHttpObserver<BaseResultDataInfo<LedimUserBean>>() {
                                  @Override
                                  public void onCompleted() {
                                      super.onCompleted();
                                  }

                                  @Override
                                  public void onNext(BaseResultDataInfo<LedimUserBean> dataInfo) {
                                      super.onNext(dataInfo);
                                      mLoadingBar.dismiss();
                                      if (dataInfo != null) {
                                          if (dataInfo.success) {
                                              LedimUserFragment frament =
                                                      LedimUserFragment.getInstance(dataInfo.data,
                                                              mRoom != null && mRoom.user != null &&
                                                                      mRoom.user.userId.equals(LedimSpUtils
                                                                              .getUid()), mRoom.roomId);
                                              frament.show(((FragmentActivity) getContext()).getSupportFragmentManager(), "1");
                                          } else {
                                              //失败
                                              ToastUtil.toast(dataInfo.message);
                                          }
                                      }

                                  }

                                  @Override
                                  public void onError(Throwable e) {
                                      super.onError(e);
                                      mLoadingBar.dismiss();
                                      LogUtil.e(e.getMessage());
                                  }
                              });
    }

}
